var dataTable, currentRow = 0, map;

$(document).ready(function () {
    initializeMap('map_region');
    fixedPolygons();
    dtInit();
    boxes();
    borrarModalInit();
    $('#left-panel li[data-nav="regiones"]').addClass('active');
})

function initializeMap (container) {
    map = new google.maps.Map(document.getElementById(container), {scrollwheel: false});
    map.setCenter(new google.maps.LatLng(-34.603526, -58.381586));
    map.setZoom(5);
    map.setMapTypeId( google.maps.MapTypeId.ROADMAP );
}

function myTrim(x) {
    return x.replace(/^\s+|\s+$/gm,'');
}

function dtInit () {
    dataTable = $('#regiones').DataTable({
        processing: false,
        serverSide: true,
        stateSave: false,
        ajax: BASE_URL+'php/providers/regiones.provider.php',
        language: dtLanguage,
        columns: [
            { 'data': 'value' },
            { 'data': 'acciones' }
        ],
        fnDrawCallback: function( oSettings ) {
            borrar();
        },
        columnDefs: [
            { 
                sortable: false,
                targets: 1
            }
            
        ],
       order: [[ 0, "asc" ]]
    });
}

function borrar () {
    var id;
    $('.borrarRegion').click(function (event) {
        id = $(this).attr('data-id');
        event.preventDefault();
        borrarModalInit();
        $('#myModal').modal('show');
        $('#myModal #modalAction').click(function () {
            $('#myModal .modal-footer button').unbind('click');
            loaderModalInit();
            $.ajax({
                type:'post',
                url: BASE_URL+'php/controllers/borrarRegion.controller.php',
                data:{id:id},
                success: function () {
                    
                    $('#myModal').modal('hide');
                    $('#row'+id).fadeOut(
                        500,
                        function () {
                            $('#row'+id).remove();
                            if ($('#regiones tbody tr').length == 0) {
                                dataTable.ajax.reload();
                            }
                        }
                    )
                    
                }
            })
        })
    })
}

function borrarModalInit () {
    $('#myModal #myModalLabel .text').html('Borrar Region');
    $('#myModal #myModalLabel .jarviswidget-loader').hide();
    $('#myModal .modal-body').html('<p>¿Está seguro que desea borrar esta región?</p>');
    $('#myModal #modalAction').html('Borrar').addClass('btn-danger');
    $('#myModal .modal-footer button').attr('disabled', false);
}

function loaderModalInit () {
    $('#myModal #myModalLabel .jarviswidget-loader').show();
    $('#myModal .modal-body').html('<p>Por favor espere...</p>');
    $('#myModal .modal-footer button').attr('disabled', true);
}

function boxes () {
    if (document.location.hash == '#new') boxSuccess('La región se cargó con éxito');
    if (document.location.hash == '#edit') boxSuccess('La región se editó con éxito');
}

function fixedPolygons () {
    var polygons, latLng, coords;
    if (polygons = $('textarea[name="fixedPolygons"]').val()) {
        polygons = polygons.split(';');
        for (var n in polygons) {
            var fixedPolygon;
            coords = [];
            polygons[n] = polygons[n].split(',');
            for (var i=0, l=polygons[n].length; i<l; i++) {
                latLng = myTrim(polygons[n][i]).split(' ');
                coords.push(new google.maps.LatLng(latLng[0], latLng[1]));
            }

    
            fixedPolygon = new google.maps.Polygon({
                paths: coords,
                strokeColor: '#333333',
                strokeOpacity: 0.8,
                strokeWeight: 2,
                fillColor: '#666666',
                fillOpacity: 0.35
            });
            
            google.maps.event.addListener(fixedPolygon, 'click', function (event) {
                google.maps.event.trigger(map, 'click', event);
            });
            
            fixedPolygon.setMap(map);
        }
        
    }
}